package com.itheima.controller;

import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.StuQueryParam;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/students")
@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    //分页查询学员信息
    @GetMapping
    public Result list(StuQueryParam stuQueryParam) {
        log.info("查询所有学员信息");
        PageResult<Student>  studentList = studentService.list(stuQueryParam);
        return Result.success(studentList);
    }

    //添加学员
    @PostMapping
    public Result save(@RequestBody Student student) {
        log.info("添加学员信息：{}",student);
        studentService.save(student);
        return Result.success();
    }
    //根据id查询学员信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询学员信息，参数：{}",id);
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    //修改学员信息
    @PutMapping
    public Result update(@RequestBody Student student) {
        log.info("修改学员信息：{}",student);
        studentService.update(student);
        return Result.success();
    }

    //批量删除学员信息
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("批量删除学员信息，参数：{}",ids);
        studentService.delete(ids);
        return Result.success();
    }

    //学员违纪处理，违纪处理一次，需要将违纪次数+1，违纪扣分+前端输入的分数。
    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id,@PathVariable Short score) {
        log.info("违纪处理，参数：{},{}",id,score);
        studentService.violation(id,score);
        return Result.success();
    }

}
